import { RouteRecordName, createRouter, createWebHashHistory } from 'vue-router'
import routes from './routes'
import { getToken } from '../utils/session'
import { ElMessage } from 'element-plus'
import * as NProgress from 'nprogress'

const router = createRouter({
  history: createWebHashHistory(),
  routes
})
NProgress.configure({ showSpinner: false })
// @ts-ignore
router.beforeEach(async (to, from , next) => {
  NProgress.start()
  if(!router.hasRoute(to.name as RouteRecordName)) {
    next('/404')
    return
  }
  if(to.meta.isAuth) {
    if(getToken()) {
      next()
      return
    } else {
      ElMessage({
        type: 'warning',
        message: '未登录'
      })
      next('/login')
      return
    }
  }
  next()
})

router.afterEach(() => {
  NProgress.done()
})
export default router

